package fi.helsinki.cs.ohtu10k.view;

import java.util.Calendar;
import java.util.GregorianCalendar;

/**
 * Utility class with tools for calendar related calculation.
 * @author Thomas Nyman
 *
 */
public final class Util {
	
	/**
	 * Returns number of days in specified month.
	 * @param month Month number, must be in range 1-12. 
	 * @return Number of days in specified month.
	 * @throws IllegalArgumentExceptino if month value out of range.
	 */
	public static int daysInMonth(int month) {
		if ((month < 1) || (month > 12)) 
			throw new IllegalArgumentException("Month number out of range: " + month);
		
		return new GregorianCalendar(Calendar.getInstance().get(Calendar.YEAR), 
    								 month-1, 1).getActualMaximum(Calendar.DAY_OF_MONTH);
	}
	
	/**
	 * Returns an array of ordered integers in specified range.
	 * If from is less that to, the integers will be in reverse order.
	 * @param from 	Starting value.
	 * @param to 	Ending value
	 * @return 		Array of ordered integers in specified range.
	 */
	public static Integer[] arrayFill(int from, int to) {
		Integer[] array;
		if (from > to) {
			array = new Integer[(from-to) + 1];
			for (int i = from, j = 0; i <= to; i--, j++)
				array[j] = i; 
		} else {
			array = new Integer[(to-from) + 1];
			for (int i = from, j = 0; i <= to; i++, j++)
				array[j] = i;
		}
		return array;
	}

}
